import { Outfit } from 'next/font/google';
import Script from 'next/script';
import './theme-init.css'; // 先导入初始主题样式
import './globals.css';

import { SidebarProvider } from '@/context/SidebarContext';
import { ThemeProvider } from '@/context/ThemeContext';
import { NotificationProvider } from '@/context/NotificationContext';

// Import services initialization to trigger auto-start
// 这个导入确保服务在应用启动时就被初始化
import '@/lib/services-init';

const outfit = Outfit({
  subsets: ["latin"],
});

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en" suppressHydrationWarning>
      <head>
        <meta name="color-scheme" content="light dark" />
        <script src="/noflash.js"></script>
        <Script id="theme-script" strategy="afterInteractive">
          {`
            // 监听系统主题变化
            if (window.matchMedia) {
              window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
                if (!localStorage.getItem('theme')) {
                  if (e.matches) {
                    document.documentElement.classList.add('dark');
                  } else {
                    document.documentElement.classList.remove('dark');
                  }
                }
              });
            }
          `}
        </Script>
      </head>
      <body className={`${outfit.className} dark:bg-zinc-800`} suppressHydrationWarning>
        <ThemeProvider>
          <NotificationProvider>
          <SidebarProvider>{children}</SidebarProvider>
          </NotificationProvider>
        </ThemeProvider>
      </body>
    </html>
  );
}
